home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / C-H / CMacPrimer.cpt / Zinger ƒ / zinger.c < prev   
C/C++ Source or Header  |  1990-03-18  |  5KB  |  219 lines

  1. #define BASE_RES_ID            400
  2. #define NIL_POINTER            0L
  3. #define MOVE_TO_FRONT        -1L
  4. #define REMOVE_ALL_EVENTS    0
  5.  
  6. #define MIN_SLEEP            0L
  7. #define NIL_MOUSE_REGION    0L
  8.  
  9. #define  DRAG_THRESHOLD        30
  10.  
  11. #define WNE_TRAP_NUM        0x60
  12. #define UNIMPL_TRAP_NUM        0x9F
  13.  
  14. #define POPUP_MENU_ID        BASE_RES_ID
  15. #define NOT_A_NORMAL_MENU    -1
  16.  
  17. #define POPUP_LEFT            100
  18. #define POPUP_TOP            35
  19. #define POPUP_RIGHT            125
  20. #define POPUP_BOTTOM        52
  21. #define SHADOW_PIXELS        1
  22. #define RIGHT_MARGIN        5
  23. #define BOTTOM_MARGIN        4
  24. #define LEFT_MARGIN            5
  25. #define PIXEL_FOR_TOP_LINE    1
  26.  
  27.  
  28. Boolean            gDone, gWNEImplemented;
  29. int                gPopUpItem = 1, gPopUpLabelWidth;
  30. MenuHandle        gPopUpMenu;
  31. EventRecord        gTheEvent;
  32. Rect            gPopUpRect, gLabelRect, gDragRect;
  33.  
  34. /****************** Main ****************************/
  35.  
  36. main()
  37. {
  38.     ToolBoxInit();
  39.     WindowInit();
  40.     SetUpDragRect();
  41.     MenuBarInit();
  42.     DrawPopUp();
  43.     MainLoop();
  44. }
  45.  
  46. /**************** ToolBoxInit ***********************/
  47.  
  48. ToolBoxInit()
  49. {
  50.     InitGraf (&thePort);
  51.     InitFonts();
  52.     FlushEvents (everyEvent, REMOVE_ALL_EVENTS);
  53.     InitWindows();
  54.     InitMenus();
  55.     TEInit();
  56.     InitDialogs(NIL_POINTER);
  57.     InitCursor();
  58. }
  59.  
  60. /************************* WindowInit ****************/
  61.  
  62. WindowInit ()
  63. {
  64.     WindowPtr    popUpWindow;
  65.     
  66.     popUpWindow = GetNewWindow (BASE_RES_ID, NIL_POINTER, MOVE_TO_FRONT);
  67.     
  68.     ShowWindow(popUpWindow);
  69.     SetPort(popUpWindow);
  70.     TextFont(systemFont);
  71.     TextMode(srcCopy);
  72. }
  73.  
  74. /******************* SetUpDragRect **********************/
  75.  
  76. SetUpDragRect()
  77. {
  78.     gDragRect = screenBits.bounds;
  79.     gDragRect.left += DRAG_THRESHOLD;
  80.     gDragRect.right -= DRAG_THRESHOLD;
  81.     gDragRect.bottom -= DRAG_THRESHOLD;
  82. }
  83.  
  84. /************** MenuBarInit ******************************/
  85.  
  86. MenuBarInit()
  87. {
  88.     gPopUpMenu = GetMenu (POPUP_MENU_ID);
  89.     InsertMenu(gPopUpMenu, NOT_A_NORMAL_MENU);
  90.     HLock(gPopUpMenu);
  91.     gPopUpLabelWidth = StringWidth ((**gPopUpMenu).menuData);
  92.     HUnlock(gPopUpMenu);
  93. }
  94.  
  95. /*************** DrawPopUp ********************************/
  96.  
  97. DrawPopUp()
  98. {
  99.     SetRect (&gPopUpRect, POPUP_LEFT,POPUP_TOP,POPUP_RIGHT,POPUP_BOTTOM);
  100.     FrameRect (&gPopUpRect);
  101.     
  102.     MoveTo (gPopUpRect.left+SHADOW_PIXELS, gPopUpRect.bottom);
  103.     LineTo (gPopUpRect.right, gPopUpRect.bottom);
  104.     LineTo (gPopUpRect.right, gPopUpRect.top+SHADOW_PIXELS);
  105.     
  106.     MoveTo (gPopUpRect.left - gPopUpLabelWidth - RIGHT_MARGIN,
  107.                         gPopUpRect.bottom - BOTTOM_MARGIN);
  108.     HLock(gPopUpMenu);
  109.     DrawString ((**gPopUpMenu).menuData);
  110.     HUnlock(gPopUpMenu);
  111.                         
  112.     gLabelRect.top = gPopUpRect.top + PIXEL_FOR_TOP_LINE;
  113.     gLabelRect.left = gPopUpRect.left - gPopUpLabelWidth
  114.                                 - LEFT_MARGIN - RIGHT_MARGIN;
  115.     gLabelRect.right = gPopUpRect.left;
  116.     gLabelRect.bottom = gPopUpRect.bottom;
  117.     
  118.     DrawPopUpNumber();
  119. }
  120.  
  121. /********************** DrawPopUpNumber ***********************/
  122.  
  123. DrawPopUpNumber()
  124. {
  125.     Str255    menuItem;
  126.     int        itemLeftMargin;
  127.     
  128.     GetItem (gPopUpMenu, gPopUpItem, &menuItem);
  129.     itemLeftMargin = (gPopUpRect.right - gPopUpRect.left -
  130.                         StringWidth(menuItem))/2;
  131.     MoveTo (gPopUpRect.left + itemLeftMargin, 
  132.                             gPopUpRect.bottom - BOTTOM_MARGIN);
  133.     DrawString(menuItem);
  134. }
  135.  
  136. /******************* MainLoop ******************************/
  137.  
  138. MainLoop()
  139. {
  140.     gDone = FALSE;
  141.     gWNEImplemented = (NGetTrapAddress(WNE_TRAP_NUM, ToolTrap) !=
  142.                         NGetTrapAddress(UNIMPL_TRAP_NUM, ToolTrap));
  143.     while(gDone == FALSE)
  144.     {
  145.         HandleEvent();
  146.     }
  147. }
  148.  
  149. /*******************HandleEvent***************/
  150.  
  151. HandleEvent()
  152. {
  153.     if(gWNEImplemented)
  154.             WaitNextEvent (everyEvent, &gTheEvent,MIN_SLEEP,
  155.                                             NIL_MOUSE_REGION);
  156.     else
  157.     {
  158.         SystemTask();
  159.         GetNextEvent(everyEvent,&gTheEvent);
  160.     }
  161.     
  162.     switch (gTheEvent.what)
  163.     {
  164.         case mouseDown:
  165.             HandleMouseDown();
  166.             break;
  167.         case updateEvt:
  168.             BeginUpdate(gTheEvent.message);
  169.             DrawPopUp();
  170.             EndUpdate(gTheEvent.message);
  171.             break;
  172.     }
  173. }
  174.  
  175. /********************** HandleMouseDown ***********************/
  176.  
  177. HandleMouseDown()
  178. {
  179.     WindowPtr    whichWindow;
  180.     short int    thePart, i;
  181.     long int    theChoice;
  182.     Point        myPoint, popUpUpperLeft;
  183.     
  184.     thePart = FindWindow (gTheEvent.where, &whichWindow);
  185.     switch(thePart)
  186.     {
  187.         case inContent:
  188.                 myPoint = gTheEvent.where;
  189.                 GlobalToLocal (&myPoint);
  190.                 if ( PtInRect(myPoint,&gPopUpRect))
  191.                 {
  192.                     InvertRect(&gLabelRect);
  193.                     popUpUpperLeft.v = gPopUpRect.top +PIXEL_FOR_TOP_LINE;
  194.                     popUpUpperLeft.h = gPopUpRect.left;
  195.                     LocalToGlobal(&popUpUpperLeft);
  196.                     theChoice = PopUpMenuSelect(gPopUpMenu,
  197.                             popUpUpperLeft.v, popUpUpperLeft.h,
  198.                             gPopUpItem);
  199.                     InvertRect (&gLabelRect);
  200.                     if (LoWord(theChoice)>0)
  201.                     {
  202.                         gPopUpItem = LoWord(theChoice);
  203.                         DrawPopUpNumber();
  204.                         for(i=0;i<gPopUpItem;i++)
  205.                             SysBeep(20);
  206.                     }
  207.                 }
  208.                 break;
  209.         case inSysWindow:
  210.             SystemClick(&gTheEvent,whichWindow);
  211.             break;
  212.         case inDrag:
  213.             DragWindow(whichWindow,gTheEvent.where,&gDragRect);
  214.             break;
  215.         case inGoAway:
  216.             gDone = TRUE;
  217.             break;
  218.     }
  219. }